package cn.lingnan.service.serviceimpl;

import cn.lingnan.mapper.AdminMapper;
import cn.lingnan.mapper.UserMapper;
import cn.lingnan.pojo.dto.PropertyDTO;
import cn.lingnan.pojo.entity.PageBean;
import cn.lingnan.pojo.entity.Property;
import cn.lingnan.pojo.vo.TransactionVO;
import cn.lingnan.pojo.vo.UserVO;
import cn.lingnan.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public List<TransactionVO> getHouseTransaction(Integer type) {
        return adminMapper.getHouseTransaction(type) ;
    }

    @Override
    public void agreeTransaction(Long transactionId) {
        adminMapper.agreeTransaction(transactionId);
    }

    @Override
    public void savePropertyInfo(PropertyDTO propertyDTO) {
        Property property = new Property();
        BeanUtils.copyProperties(propertyDTO, property);
        adminMapper.savePropertyInfo(property);
    }

    @Override
    public PageBean getAllUserInfo(int pagenum,int pagesize) {
        PageHelper.startPage(pagenum,pagesize);

        Page<UserVO> page = adminMapper.getAllUserInfo();
        long total = page.getTotal();
        List<UserVO> list = page.getResult();
        return new PageBean(total,list);
    }

    @Override
    public void toggleUserStatus(Long userId, Integer status) {
        adminMapper.toggleUserStatus(userId,status);
    }

    @Override
    @Transactional
    public void deleteUser(Long[] userIds) {
        for (Long userId : userIds) {
            adminMapper.deleteUser(userId);
        }
    }

    @Override
    public UserVO getUserInfoById(Long userId) {
        return  adminMapper.getUserInfoById(userId);
    }
}
